package com.ypf.delaycancelorder.consumer;

import com.ypf.delaycancelorder.config.DelayedQueueConfig;
import com.ypf.delaycancelorder.entity.Order;
import com.ypf.delaycancelorder.service.IOrderService;
import com.ypf.delaycancelorder.service.ex.OrderEmptyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DelayQueueConsumer {
    @Autowired
    private IOrderService orderService;
    @RabbitListener(queues = DelayedQueueConfig.DELAYED_QUEUE_NAME)
    public void receiveDelayQueue(Message message){
        String orderNo=new String(message.getBody());
//        log.info("收到的订单号为:{}",orderNo);
        Order order = orderService.findByOrderNo(orderNo);

        // 如果订单为空抛出异常
        if(order==null){
            throw new OrderEmptyException();
        }
        // 订单支付状态不为已支付时
        if (order.getStatus()!=Order.pay_status_paid){
            // 将订单状态改为取消支付
            orderService.updateByOrderNo(orderNo,Order.pay_status_cancel);
        }

    }
}
